package com.bw.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bw.bean.ActiveUser;
import com.bw.bean.Teacher;
import com.bw.bean.addBean.Classes;
import com.bw.bean.addBean.Student;
import com.bw.bean.addBean.Teachers;
import com.bw.bean.addBean.TestReturn;
import com.bw.service.AddService;
import com.bw.util.PageUtil;

@Controller
@RequestMapping("addController")
public class AddController {

	@Autowired
	private AddService addService;

	@RequestMapping("doAddStudent")
	/*@RequiresPermissions("student:add")*/
	@ResponseBody
	public boolean doAddStudent(Student student,HttpServletRequest request) {
		/*String sname = request.getParameter("sname");
		System.err.println(sname+"============");
		String cid = request.getParameter("cid");
		String job_status = request.getParameter("job_status");
		String call_status = request.getParameter("call_status");
		student.setSname(sname);
		student.setCid(Integer.valueOf(cid));
		student.setJob_status(Integer.valueOf(job_status));
		student.setCall_status(Integer.valueOf(call_status));*/
		int msg = 0;
		try {
			msg = addService.doAddStudent(student);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}
		if (msg > 0) {
			return true;
		} else {
			return false;
		}
	}

	@RequestMapping("doAddClasses")
	/*@RequiresPermissions("class:add")*/
	@ResponseBody
	public boolean doAddClasses(Classes classes,HttpServletRequest request) {
		String cname = request.getParameter("cname");
		String status = request.getParameter("status");
		String tid = request.getParameter("tid");
		
		classes.setCname(cname);
		classes.setStatus(Integer.valueOf(status));
		classes.setTid(Integer.valueOf(tid));
		int msg = 0;
		try {
			msg = addService.doAddClasses(classes);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}
		if (msg > 0) {
			return true;
		} else {
			return false;
		}

	}

	@RequestMapping("doAddTeacher")
	/*@RequiresPermissions("teacher:add")*/
	@ResponseBody
	public boolean doAddTeacher( HttpServletRequest request,Teachers teachers) {
		String username = request.getParameter("username");
		String type = request.getParameter("type");
		String remarks = request.getParameter("remarks");
		teachers.setUsername(username);
		teachers.setType(Integer.valueOf(type));
		teachers.setRemarks(remarks);
		int msg = 0;
		try {
			msg = addService.doAddTeachers(teachers);
		} catch (Exception e) { // TODO Auto-generated catch block
			e.printStackTrace();

		}

		if (msg > 0) {
			
			return true;
		} else {
			return false;
		}
	}

	@RequestMapping("jumpStudent")
	public String ccc(){
		return "/listStudent";
	}
	
	@RequestMapping("jumpTeacher")
	public String aaa() {
		return "/listTeacher";
	}

	@RequestMapping("jumpClasses")
	public String bbb() {
		return "/listClasses";
	}
	
	
	@RequestMapping("listStudent")
	@ResponseBody
	public Map<String,Object> listStudent(HttpServletRequest request,int page,int rows) {
		ActiveUser user = (ActiveUser)request.getSession().getAttribute("activeUser");
		Map<String,Object> map = new HashMap<String,Object>();
		int count = addService.getCountByStudent();
		PageUtil pageUtil = new PageUtil(page+"",count,rows);
		List<Student> list = addService.getListStudent(pageUtil, user.getTid());
		map.put("total", count);
		map.put("rows", list);
		return map;
	}
	

	@RequestMapping("listTeacher")
	@ResponseBody
	public Map<String,Object> listTeacher(HttpServletRequest request,int page,int rows) {
		Map<String,Object> map = new HashMap<String,Object>();
		int count = addService.getCountByTeachers();
		PageUtil pageUtil = new PageUtil(page+"",count,rows);
		List<Teachers> list = addService.getListTeacher(pageUtil);
		map.put("total", count);
		map.put("rows", list);
		return map;
	}

	@RequestMapping("listClasses")
	@ResponseBody
	public Map<String,Object> listClasses(HttpServletRequest request,int page,int rows) throws Exception {
		ActiveUser user = (ActiveUser)request.getSession().getAttribute("activeUser");
		Map<String,Object> map = new HashMap<String,Object>();
		int count = addService.getCountByClasses();
		PageUtil pageUtil = new PageUtil(page+"",count,rows);
		List<Classes> listClasses = addService.getListClasses(pageUtil,user.getTid());
		map.put("total", count);
		map.put("rows", listClasses);
		return map;
	}

	@RequestMapping("listByTeacher")
	@ResponseBody
	public List<TestReturn> listteacher() throws Exception{
		List<TestReturn> teacher = addService.getTeacher();
		teacher.removeIf(new Predicate<TestReturn>() {
			@Override
			public boolean test(TestReturn t) {
				// TODO Auto-generated method stub
				return t.getId()==-1;
			}
		});
		return teacher;
	}
	
	@RequestMapping("listByClasses")
	@ResponseBody
	public List<TestReturn> listclasses() throws Exception{
		List<TestReturn> classses = addService.getClasses();
		
		return classses;
	}
	
	
}
